/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
/**
 * Software License, Version 1.0
 * 
 * Copyright 2004 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.gridapps.nbgridftp.publisher;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
/**
   @author Sang Boem Lim
   $Date$
   $Revision$
 */
public class NBProgress extends Thread {

    private int numTasks = 0;
    private ProgressBarDemo newContentPane;
    private boolean isDone;

    public NBProgress(int numTasks, String fileName) {
        
        this.numTasks = numTasks;
        isDone = false;

        newContentPane = new ProgressBarDemo(numTasks, fileName);
    }

    public void setup(int i, int workUnits) {
        newContentPane.setup(i, workUnits);
    }

    public void update(int i, int complete) {
        newContentPane.update(i, complete);
    }

    public void setDone() {
        isDone = true;
    }

    private void makeCenter(JFrame f, int width, int height) {

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width-width)/2;
        int y = (screen.height-height)/2;

        f.setBounds(x,y,width,height);
    }

    public void run() {

        JFrame frame = new JFrame("Publisher Progress Monitor.");
        makeCenter(frame, 300, 200);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        //Create and set up the content pane.
        newContentPane.create();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);

        while(true) {
            try{
                Thread.sleep(500);
            } catch (InterruptedException err){}
            
            if(isDone) break;
        }
        frame.setVisible(false);
        frame.dispose();
    }
}

class  ProgressBarDemo extends JPanel {

    private JProgressBar pbar0, pbar1, pbar2, pbar3;
    private int numTasks;
    private String fileName;

    public ProgressBarDemo(int numTasks, String fileName) {

        super(new GridLayout(numTasks + 2, 1));
        this.numTasks = numTasks;
        this.fileName = fileName;
    }
    
    public void setup(int i, int workunits) {

        switch (i) {
            case 3:
                pbar3 = new JProgressBar(0, workunits);
                pbar3.setValue(0);
                pbar3.setStringPainted(true);
                break;
            case 2:
                pbar2 = new JProgressBar(0, workunits);
                pbar2.setValue(0);
                pbar2.setStringPainted(true);
                break;
            case 1:
                pbar1 = new JProgressBar(0, workunits);
                pbar1.setValue(0);
                pbar1.setStringPainted(true);
                break;
            case 0:
                pbar0 = new JProgressBar(0, workunits);
                pbar0.setValue(0);
                pbar0.setStringPainted(true);
                break;
        }
    }

    public void create() {

        add(new JLabel("Sending  \"" + fileName + "\"  using reliable NaradaBrokering."));
        add(new JLabel("This job contains " + numTasks +
                       " sub-tasks."));
        switch (numTasks) {
            case 4:
                JPanel panel4 = new JPanel();
                panel4.add(new JLabel("Task number 4."));
                panel4.add(pbar3);
                
                add(panel4);//, BorderLayout.PAGE_START);
            case 3:
                JPanel panel3 = new JPanel();
                panel3.add(new JLabel("Task number 3."));
                panel3.add(pbar2);
                
                add(panel3);//, BorderLayout.PAGE_START);

            case 2:
                JPanel panel2 = new JPanel();
                panel2.add(new JLabel("Task number 2."));
                panel2.add(pbar1);
                
                add(panel2);//, BorderLayout.PAGE_START);

            case 1:
                JPanel panel1 = new JPanel();
                panel1.add(new JLabel("Task number 1."));
                panel1.add(pbar0);
                
                add(panel1);//, BorderLayout.PAGE_START);
        }
    }

    public void update(int i, int complete) {
        switch (i) {
            case 3:
                pbar3.setValue(complete);
                break;
            case 2:
                pbar2.setValue(complete);
                break;
            case 1:
                pbar1.setValue(complete);
                break;
            case 0:
                pbar0.setValue(complete);
                break;
        }
    }
}

